<?php include '_init_.php';?>
<?php
$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
$dt = new DataTable(constant('DB_TABLE_USER'));
 $errorMessage  = '';
if($act == 'save')
{
	try
   {
	$data = $_REQUEST;
	$data = checkInfo($data);
	if(strlen($data['login_password']) > 0)
	{
		$data['login_password'] = md5($data['login_password']);
	}else{
		unset($data['login_password']);
	}
	if(intval($data['id']) < 1)
	{
		$data['is_active'] = 'Y';
		unset($data['id']);
	}
	$dt->save($data);
	AppMain::refreshParentFrame();
	}catch(Exception $e)
	{
		  $errorMessage = $e->getMessage();
	}
}
if($act == 'edit')
{
	$info = $dt->get(intval($_REQUEST['id']));
	if($info['is_admin'] == 'Y')
	{
		$info['type'] = 0;
	}
	if($info['is_csr'] == 'Y')
	{
		$info['type'] = 1;
	}
	if($info['is_photo'] == 'Y')
	{
		$info['type'] = 2;
	}
	if($info['is_product'] == 'Y')
	{
		$info['type'] = 3;
	}
}
if($act == 'new')
{
	$info = array('id'=>'','name'=>'','type'=>$_REQUEST['type']);
}

function checkInfo($info)
{

		if(strlen(trim($_REQUEST['login_user'])) == 0)
			throw new Exception('帳號不能够爲空');
		global $dt;
		$condition = sprintf("`login_user` = '%s'",mysql_real_escape_string($_REQUEST['login_user']));
		$exist_user = $dt->get_row($condition);
		if($exist_user != null && $exist_user['id'] != $info['id'])
			throw new Exception(sprintf(" '%s' 已經存在 請選用另外一個名稱.",$_REQUEST['login_user']));
			
			
	$password = mysql_real_escape_string($_POST['login_password']);
	$password2 = mysql_real_escape_string($_POST['password_confirm']);
	$error = '';

    if(strlen($password ) > 0)
    {
		if($password != $password2) {
    		$error = '確認密碼不對.';
		}
	
		if(strlen($password) < 3) {
    		$error = '密碼至少是3個字符.';
		}
	}
	if(strlen($error) > 0)
	{
		throw new Exception($error);
	}
	
	if($_REQUEST['type'] == 0)
	{
		$info['is_admin'] = 'Y';
		$info['is_csr'] = 'N';
		$info['is_photo'] = 'N';
		$info['is_product'] = 'N';
	}
		if($_REQUEST['type'] == 1)
	{
		$info['is_admin'] = 'N';
		$info['is_csr'] = 'Y';
		$info['is_photo'] = 'N';
		$info['is_product'] = 'N';
	}
		if($_REQUEST['type'] == 2)
	{
		$info['is_admin'] = 'N';
		$info['is_csr'] = 'N';
		$info['is_photo'] = 'Y';
		$info['is_product'] = 'N';
	}
		if($_REQUEST['type'] == 3)
	{
		$info['is_admin'] = 'N';
		$info['is_csr'] = 'N';
		$info['is_photo'] = 'N';
		$info['is_product'] = 'Y';
	}
	
	return $info;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/formvalidate.js"></script>
<script type="text/javascript">
$(function(){
	$('select[name=type]').val('<?php echo Util::html_safe($info['type']);?>');
});
</script>
<style>
.article-1a
{
width:380px;
}
</style>
</head>

<body>
 <?php if (strlen($errorMessage) > 0):?>
<div class="error_message"><?php echo $errorMessage;?></div>
<?php endif;?>
<form method="post">
<input type="hidden" name="act" value="save"/>
<input type="hidden" name="isadmin" value="<?php echo Util::html_safe($_REQUEST['isadmin']);?>"/>
<div id="article-admin" class="clearfix">
  <ul>
  <?php if($_REQUEST['isadmin'] != '1'):?>
  <li class="article-1a">
     部門<select name="type">
     	<option value="1">門市部及客服部</option>
     	<option value="2">攝影部及造型部</option>
     	<option value="3">製作部</option>
     </select></li>
     <?php else:?>
     <input type="hidden" name="type" value="0"/>
    <?php endif;?>
     <li class="article-1a">
    帳號<input name="login_user" type="text" class="maine-keyina" value="<?php echo Util::html_safe($info['login_user']);?>"/></li>
      <li class="article-1a">
    密碼<input name="login_password" type="password" class="maine-keyina"/></li>
      <li class="article-1a">
    確認<input name="password_confirm" type="password" class="maine-keyina"/></li>
     <li class="article-1b">
       <input name="" type="button" class="article-1a_btn" value="儲存" onclick="this.form.submit();"/></li>
  </ul>

</div>
</form>
</body>
</html>
